/*
 * This file is part of connotea-java.
 *
 * connotea-java is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * connotea-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.connotea;

import static org.apache.commons.lang.builder.ToStringBuilder.reflectionToString;

import java.io.Serializable;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Citation author entity bean.
 * 
 * @author <a href="mailto:christopher.townson@googlemail.com">Christopher
 *         Townson</a>
 */
public final class Author implements Comparable<Author>, Serializable {

    private static final long serialVersionUID = 1L;

    private String firstName;

    private String lastName;

    /**
     * Default, no-argument constructor.
     */
    public Author() {
        // do nothing
    }

    /**
     * Copy constructor.
     * 
     * @param author the author to copy
     */
    public Author(Author author) {
        setFirstName(author.getFirstName());
        setLastName(author.getLastName());
    }

    /**
     * Instantiate an Author, setting the provided values for firstName, and
     * lastName.
     * 
     * @param firstName the firstName to set
     * @param lastName the lastName to set
     */
    public Author(String firstName, String lastName) {
        setFirstName(firstName);
        setLastName(lastName);
    }

    /**
     * Comparability is based on lastName, then firstName.
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Author author) {
        return new CompareToBuilder().append(lastName, author.lastName).append(
                firstName, author.firstName).toComparison();
    }

    /**
     * Equality is based on lastName, then firstName.
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Author)) {
            return false;
        }
        Author author = (Author) obj;
        return new EqualsBuilder().append(lastName, author.lastName).append(
                firstName, author.firstName).isEquals();
    }

    /**
     * Returns the author's firstName(s).
     * 
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Returns the author's lastName.
     * 
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * HashCode is based on lastName, then firstName.
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(lastName).append(firstName)
                .toHashCode();
    }

    /**
     * Sets the firstName.
     * 
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Sets the lastName.
     * 
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return reflectionToString(this);
    }
}
